package cn.tedu.hospital.management.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.hospital.management.dao.persist.repository.*;
import cn.tedu.hospital.management.pojo.entity.*;
import cn.tedu.hospital.management.pojo.param.*;
import cn.tedu.hospital.management.pojo.vo.HospitalHospitalVO;
import cn.tedu.hospital.management.pojo.vo.HospitalStandardVO;
import cn.tedu.hospital.management.pojo.vo.HospitalVO;
import cn.tedu.hospital.management.service.IHospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class HospitalServiceImpl implements IHospitalService {

    @Autowired
    private IHospitalRepository hospitalRepository;

    @Autowired
    private IHospitalFieldRepository hospitalFieldRepository;

    @Override
    public void insert(HospitalAddNewParam param) {
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(param,hospital);
        hospitalRepository.insert(hospital);

        Field field = new Field();
        field.setHospitalId(hospitalRepository.hospitalIdByName(param.getName()));
        field.setHospitalCityId(hospitalRepository.cityIdByName(param.getCityName()));
        field.setHospitalLevelId(hospitalRepository.levelIdByName(param.getLevelName()));
        field.setHospitalProvinceId(hospitalRepository.provinceIdByName(param.getProvinceName()));
        field.setHospitalTypeId(hospitalRepository.typeIdByName(param.getTypeName()));
        hospitalRepository.insert(field);
    }

    @Override
    public void delectById(Long id) {
        hospitalRepository.delectById(id);
    }

    @Override
    public void updateById(Long id, HospitalUpdateParam hospitalUpdateParam) {
        City city = new City();
        city.setId(id);
        city.setName(hospitalUpdateParam.getCityName());
        hospitalRepository.updateCityIdBy(city);
        Hospital hospital = new Hospital();
        hospital.setId(hospitalUpdateParam.getId());
        hospital.setLocation(hospitalUpdateParam.getLocation());
        hospital.setIntroduce(hospitalUpdateParam.getIntroduce());
        hospital.setName(hospitalUpdateParam.getName());
        hospitalRepository.updateHospitalIdBy(hospital);
        Level level = new Level();
        level.setId(id);
        level.setName(hospitalUpdateParam.getLevelName());
        hospitalRepository.updateLevelIdBy(level);
        Province province = new Province();
        province.setId(id);
        province.setName(hospitalUpdateParam.getProvinceName());
        hospitalRepository.updateProvinceIdBy(province);
        Type type = new Type();
        type.setId(id);
        type.setName(hospitalUpdateParam.getTypeName());
        hospitalRepository.updateTypeIdBy(type);


    }

    @Override
    public List<HospitalHospitalVO> listAll() {
        return hospitalRepository.selectAll();
    }


    @Override
    public void addNew(HospitalAddNewParam hospitalAddNewParam) {
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalAddNewParam,hospital);
        hospitalRepository.insertHospital(hospital);

        HospitalField hospitalField=new HospitalField();
        hospitalField.setHospitalId(hospital.getId());
        hospitalField.setHospitalCityId(hospitalRepository.selectHospitalCityId("石家庄市"));
        hospitalField.setHospitalLevelId(hospitalRepository.selectHospitalLevelId("二级甲等"));
        hospitalField.setHospitalProvinceId(hospitalRepository.selectHospitalProvinceId("河北省"));
        hospitalField.setHospitalTypeId(hospitalRepository.selectHospitalTypeId("综合医院"));
        hospitalFieldRepository.insertHospitalField(hospitalField);



    }

    /*根据id删除医院*/
    @Override
    public void delete(Long id) {
        int rows = hospitalRepository.deleteById(id);
        if (rows!=1){
            String message="删除医院失败,服务器忙,请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(Long id, HospitalUpdateInfoParam hospitalUpdateInfoParam) {
        HospitalStandardVO standardVO=hospitalRepository.getStandardById(id);
        if (standardVO==null){
            String message="修改医院信息失败,医院数据不存在!";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalUpdateInfoParam,hospital);
        hospital.setId(id);
        int rows = hospitalRepository.updateById(hospital);
        if (rows!=1){
            String message="修改医院信息失败,请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public HospitalStandardVO getStandardById(Long id) {

        HospitalStandardVO standardVO = hospitalRepository.getStandardById(id);
        if (standardVO==null){
            String message="查询医院详情失败,医院信息不存在!";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return standardVO;
    }

    @Autowired
    ProvinceRepository provinceRepository;
    @Autowired
    CityRepository cityRepository;
    @Autowired
    LevelRepository levelRepository;
    @Autowired
    TypeRepository typeRepository;

    @Override
    public List<Province> getProvince() {
        return provinceRepository.getAllProvince();
    }

    @Override
    public List<City> getSmallCity(int pid) {
        return cityRepository.getSmallCity(pid);
    }

    @Override
    public List<Level> getAllLevel() {
        return levelRepository.getAllLevel();
    }

    @Override
    public List<Type> getAllType() {
        return typeRepository.getAllType();
    }

    @Override
    public List<HospitalVO> getHospitals(HospitalParam hospitalParam) {
        return hospitalRepository.getHospitals(hospitalParam);
    }

    @Override
    public void avatar(String name, HosAddNewParam hosAddNewParam) {
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hosAddNewParam,hospital);
        hospital.setName(name);
        int rows = hospitalRepository.avatar(hospital);

        if (rows!=1){
            String message = "修改失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

}
